package com.yunshang.shoppingmall.shopping.CartController;

import com.yunshang.shoppingmall.common.MessageConstants;
import com.yunshang.shoppingmall.common.Response;
import com.yunshang.shoppingmall.common.loginOut;
import com.yunshang.shoppingmall.entity.Cart;
import com.yunshang.shoppingmall.entity.OrderDetailsProduct;
import com.yunshang.shoppingmall.entity.User;
import com.yunshang.shoppingmall.entity.UserNo;
import com.yunshang.shoppingmall.service.CartService;
import com.yunshang.shoppingmall.service.OrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yunshang.shoppingmall.common.OrderHelper.addCartPub;
import static com.yunshang.shoppingmall.common.OrderHelper.operOrderDetailsProduct;

@RequestMapping("/wap")
@Controller()
public class CartAPIController {
    @Autowired
    CartService cartService;
    @Autowired
    OrderDetailsService orderDetailsService;
    //ss
//    @ResponseBody
//    @RequestMapping("/getCartList")
//    //前端购物车列表
//    public Response getCartList(HttpServletRequest request)
//    {
//        HttpSession session = request.getSession();
//        UserNo userNo1 = (UserNo)session.getAttribute("userNo");
//        int memberUserId=0;
//        if(userNo1!=null)
//        {
//            memberUserId=userNo1.getId();
//        }
//        //System.out.println(memberUserId+"--------");
//        List<OrderDetailsProduct> list=orderDetailsService.selectCartByUserID(memberUserId+"");
//
//        if(list.size()>0)
//        {
//            return new Response(MessageConstants.MSG_SUCCESS,list,"获取成功!");
//        }
//        else
//        {
//            return new Response(MessageConstants.MSG_OPERATE_FAILED,list,"获取失败!");
//        }
//    }


    @RequestMapping("/getCartList")
    public ModelAndView getCartList(HttpServletRequest request){
        String htmlName="shoppingApi/cart/cartList";
        HttpSession session = request.getSession();
        UserNo userNo1 = (UserNo) session.getAttribute("userNo");

       //List<User> userList = (List<User>) session.getAttribute("userList");

        int memberUserId=0;
        int memberLevel=0;
        if(userNo1==null){
            htmlName="shoppingApi/user/loginbypassword";
            ModelAndView modelAndView=new ModelAndView(htmlName);
            return  modelAndView;
        }else{
            memberUserId=userNo1.getId();
            memberLevel= userNo1.getRank();//会员等级Id
        }
//
//        if (!loginOut.checkLogin(request)) {
//            userNo1=null;
//            htmlName="shoppingApi/user/loginbyphone";
//            ModelAndView modelAndView=new ModelAndView(htmlName);
//            return  modelAndView;
//        }
//        else {
//            memberUserId=userNo1.getId();
//            memberLevel= userNo1.getRank();
//        }
        Map<String,Object> map;
        ModelAndView modelAndView=new ModelAndView(htmlName);
        List<OrderDetailsProduct> list =orderDetailsService.selectCartByUserID(memberUserId);
            map = operOrderDetailsProduct(list, memberLevel, memberUserId);
        modelAndView.addObject("list",map);
        return  modelAndView;
    }


    @ResponseBody
    @RequestMapping("/addCart")
    //添加购物车
    public Response addCart(HttpServletRequest request,int productId, int guigeId, int productCount) {
        HttpSession session = request.getSession();
        UserNo userNo1 = (UserNo)session.getAttribute("userNo");
        int memberUserId=0;
        if(userNo1!=null) {
            memberUserId=userNo1.getId();
        }
        Cart cart=cartService.selectCart(productId,guigeId,memberUserId);
        if(cart==null){
            Response response=addCartPub(productId,guigeId,productCount,memberUserId);
            return response;
        } else {
            int number=cart.getProductCount()+productCount;
            cart.setProductCount(number);
            int res=cartService.updateByPrimaryKey(cart);
            if(res>0){
                return new Response(MessageConstants.MSG_SUCCESS,cart.getShoppingId(),"");
            }
            return new Response(MessageConstants.MSG_OPERATE_FAILED,0,"");
        }
    }
    @ResponseBody
    @RequestMapping("/updateCart")
    //修改购物车
    public Response updateCart(Integer shoppingId,Integer number)
    {
        Cart cart=cartService.selectByPrimaryKey(shoppingId);
        //System.out.println("---"+cart.getShoppingId());
        cart.setProductCount(number);
        int res=cartService.updateByPrimaryKey(cart);
        if(res>0)
        {
            return new Response(MessageConstants.MSG_SUCCESS,res,"");
        }
        return new Response(MessageConstants.MSG_OPERATE_FAILED,res,"");
    }
    //删除购物车
    @ResponseBody
    @RequestMapping("/deleteCart")
    public Response deleteCart(Integer shoppingId){
        Cart cart=cartService.selectByPrimaryKey(shoppingId);
        if(cart!=null){
            int state=cart.getCartStatus()==null?0:Integer.parseInt(cart.getCartStatus());
            String orderNo=cart.getOrderNo()==null?"":cart.getOrderNo();
            if(state==501||"".equals(orderNo)){
                cart.setCartStatus("502");
//                int res=cartService.deleteByPrimaryKey(shoppingId);
                int res=cartService.updateByPrimaryKey(cart);
                if(res>0){
                    return new Response(MessageConstants.MSG_SUCCESS,res,"");
                }else {
                    return new Response(MessageConstants.MSG_OPERATE_FAILED,res,"");
                }
            }else {
                return new Response(MessageConstants.MSG_SUCCESS,1,"");
            }
        }else{
            return new Response(MessageConstants.MSG_OPERATE_FAILED,0,"");
        }
    }
}
